namespace Web.Control.WWW.Wiki
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using Lx.Web.Common.UI;
    using Lx.Web.Common.ControlsUI;
    using System.Collections;
    using System.Collections.Generic;
    using System.Xml;
    using System.IO;
    using System.Text;

    using BLF;
    using BLF.WWW_WWW.Wiki;
    using Model.WWW_WWW.Wiki;

    public class EmailMessage : BaseControl
    {
        protected TextBox txtRecipient;
        protected TextBox txtSubject;
        protected TextBox txtBody;
        protected Literal lblResult;
        protected Button btnSend;
              
        protected override void OnLoad(EventArgs e)
        {
            if (SessionFacade.LoginKey == null)
            {
                Response.Redirect("/user/Login.htm?ReturnUrl=/knowledge" + Tools.UrlEncode(Request.Url.ToString().Substring(Request.Url.ToString().ToLower().IndexOf("message.htm"))));
            }

            if (!Page.IsPostBack)
            {
                btnSend.Text = GetText("Send");
                if (Request["Username"] != null)
                {
                    txtRecipient.Text = Request["Username"];
                }
                if (Request["Subject"] != null)
                {
                    txtSubject.Text = Request["Subject"];
                }
            }
            base.OnLoad(e);
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            Users u = Users.Instance;
            UserInfo user = u.Find(txtRecipient.Text);
            if (user == null)
            {
                lblResult.Text = @"<span style=""color: #FF0000;"">" + Exchanger.ResourceExchanger.GetResource("UserNotFound") + "</span>";
                return;
            }
            if (txtSubject.Text.Length == 0 || txtBody.Text.Length == 0)
            {
                lblResult.Text = @"<span style=""color: #FF0000;"">" + Exchanger.ResourceExchanger.GetResource("SubjectBodyNeeded") + "</span>";
                return;
            }
            UserInfo senderUser = u.Find(SessionFacade.Username);
            if (senderUser == null)
            {
                Response.Redirect("/user/Login.htm?ReturnUrl=/knowledge" + Tools.UrlEncode(Request.Url.ToString().Substring(Request.Url.ToString().ToLower().IndexOf("message.htm"))));
            }
            Log.LogEntry("Sending Email to " + user.Username + " from " + senderUser.Username, EntryType.General, SessionFacade.Username);
            EmailSender.Send(user.Email, "\"" + senderUser.NickName + "\"" + " <" + Settings.SenderEmail + ">", txtSubject.Text, senderUser.NickName + " sent you this message from " + Settings.WikiTitle + ". To reply, please go to " + Settings.MainUrl + "Message.htm?Username=" + senderUser.Username + "&Subject=" + Tools.UrlEncode("Re: " + txtSubject.Text) + "\nPlease do not reply to this Email.\n\n" + txtBody.Text, false);
            lblResult.Text = @"<span style=""color: #009900;"">" + Exchanger.ResourceExchanger.GetResource("MessageSent") + "</span>";
            txtSubject.Text = "";
            txtBody.Text = "";
        }
    }
}
